package com.wgz.mp.generator.controller;


import com.wgz.mp.generator.bean.User;
import com.wgz.mp.generator.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * @author wgz
 * @title
 * @Package PACKAGE_NAME
 * @data 2021/4/1 19:35
 */
@Controller
public class UserController {
    @Autowired
    IUserService userService;

    @GetMapping("user")
    public String list(Model model){
        List<User> list = userService.list();
        model.addAttribute("list",list);
        return "list";
    }

    @GetMapping("addUI")
    public String addUI(){
        return "add";
    }
    @PostMapping("add")
    public String add(User user){
        userService.save(user);
        return "redirect:/user";
    }

    @GetMapping("editUI")
    public String editUI(String id,Model model){
        User user = userService.selectUserById(id);
        model.addAttribute("user",user);
        return "edit";
    }

    @PostMapping("edit")
    public String edit(User user){
        userService.updateById(user);
        return "redirect:/user";
    }

    @GetMapping("del")
    public String del(String id){
        userService.removeById(id);
        return "redirect:/user";
    }
}

